package com.jinlejia.bbs.logic.message;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.bbs.bean.InviteBean;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.TopicBean;
import com.jinlejia.bbs.http.entity.MessageInviteEntity;
import com.jinlejia.bbs.http.entity.base.PageEntity;
import com.jinlejia.bbs.http.utils.BBSConfig;
import com.jinlejia.bbs.http.utils.HttpCallBack;
import com.jinlejia.bbs.http.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallBack;
import static com.jinlejia.bbs.http.utils.HttpUtils.httpCallForm;

/**
 * @author 王进 on 2019/4/11.
 */

public class MessageInviteListLogic {

    private final Context context;
    private ArrayList<InviteBean> dataList;
    private final LogicCallBack callBack;

    public MessageInviteListLogic(Context context, ArrayList<InviteBean> dataList, LogicCallBack callBack){
        this.context = context;
        this.dataList = dataList;
        this.callBack = callBack;
    }



    private int pageIndex;
    public void refresh(){
        pageIndex = 1;
        loadData(pageIndex);
    }

    public void load(){
        pageIndex = pageIndex+1;
        loadData(pageIndex);
    }

    private void loadData(final int pageIndex){
        JSONObject json = new JSONObject();
        json.put("resourceType",4);
        json.put("pageIndex",pageIndex);
        json.put("pageSize",20);
        httpCallForm(BBSConfig.listRemind,json).execute(httpCallBack(context, new HttpCallBack() {
            @Override
            public void onError(Exception e) {
                if (callBack==null)return;
                callBack.onResult(false);
            }

            @Override
            public void onResponse(HttpUtils.JsonParse parse) {
                if (callBack==null)return;
                TypeReference<PageEntity<MessageInviteEntity>> reference = new TypeReference<PageEntity<MessageInviteEntity>>() {
                };
                PageEntity<MessageInviteEntity> response = parse.parsePageEntity(reference);
                List<MessageInviteEntity> data = response.getData();
                ArrayList<InviteBean> list = new ArrayList<>();
                for (MessageInviteEntity entity : data) {
                    InviteBean bean = new InviteBean();
                    TopicBean topic = new TopicBean();
                    topic.setTopicTitle(entity.getContent());
                    topic.setTopicCode(entity.getResourceCode());
                    bean.setTopic(topic);

                    BBSUserBean user = new BBSUserBean();
                    user.setUserPin(entity.getTouchPin());
                    user.setUserName(entity.getTouchName());
                    bean.setInviteUser(user);

                    bean.setObjectId(entity.getObjectId());
                    list.add(bean);
                }
                if (pageIndex == 1) {
                    dataList.clear();
                }
                dataList.addAll(list);
                boolean nextPageAvailable = response.getNextPageAvailable();
                callBack.onResult(nextPageAvailable);
            }

            @Override
            public void onNoResponse(int code) {
                if (callBack==null)return;
                callBack.onResult(false);
            }
        }));
    }


    public interface LogicCallBack{
        void onResult(boolean hasNextPage);
    }
}
